package 代码随想录.哈希;

import java.util.HashMap;

public class a454四数相加2 {
    static class Solution {
        public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
            int ans = 0;
            HashMap<Integer, Integer> map = new HashMap<>();
            for (int num1 : nums1) {
                for (int num2 : nums2) {
                    map.merge(num1 + num2, 1, Integer::sum);
                }
            }
            for (int num3 : nums3) {
                for (int num4 : nums4) {
                    int target = -(num3 + num4);
                    if (map.get(target) != null) {
                        ans += map.get(target);
                    }
                }
            }
            return ans;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums1 = {1, 2}, nums2 = {-2, -1}, nums3 = {-1, 2}, nums4 = {0, 2};
        System.out.println(solution.fourSumCount(nums1, nums2, nums3, nums4));
    }
}
